Greetings and welcome to the third iteration of Humans Learning. As a reminder, each lesson is designed as a 5 - 10 minute virtual session conducted for EnCompass staff to expand their skills with data, and the means of learning is the R programming language. Each lesson will have learning objectives, some example code and explanation to demonstrate a technique or skill, and an open code chunk at the end for you to have some fun. This is all in the service of humans learning. Enjoy!
Learning objectives
For this session, the learning objectives are to:
Understand ggplotβs facetting function to make multiple plots at once
Make a facetted plot
Install and load packages
In your R script, you will use the install.packages() and library() functions to install and load the two packages Tidyverse and Gapminder.
Tidyverse provides a suite of compatible data wrangling and visualization tools. The workhorse of data visualization is the ggplot2 package. With ggplot2 the sky is the limit! From basic bar plots to animated graphics to interactive charts and tables connected by a common data source, ggplot2 and its extension packages can do it all. And once again, Gapminder provides a dataset extracted from the global trend data maintained by, https://www.gapminder.org/.
#Installs the package on your system.install.packages(c("tidyverse", "gapminder"))
Installing packages into 'C:/Users/brian/AppData/Local/R/win-library/4.4'
(as 'lib' is unspecified)
package 'tidyverse' successfully unpacked and MD5 sums checked
package 'gapminder' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\brian\AppData\Local\Temp\Rtmpwbk0yu\downloaded_packages
#load the libraries so you can use themlibrary(tidyverse)
ββ Conflicts ββββββββββββββββββββββββββββββββββββββββββ tidyverse_conflicts() ββ
β dplyr::filter() masks stats::filter()
β dplyr::lag() masks stats::lag()
βΉ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gapminder)
Set up the data
Similar to before, we need to ensure we have the data and that itβs in a format that we can use. To look at just the first six rows so you can see the variable names and structure of the data pass gapminder to head() as in the code below.
# assign gapminder to df # this is required, but it makes life easier# don't we all want life to be easierdf <- gapminder# look at the gapminder datasethead(df)
# A tibble: 6 Γ 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
tail(df)
# A tibble: 6 Γ 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Zimbabwe Africa 1982 60.4 7636524 789.
2 Zimbabwe Africa 1987 62.4 9216418 706.
3 Zimbabwe Africa 1992 60.4 10704340 693.
4 Zimbabwe Africa 1997 46.8 11404948 792.
5 Zimbabwe Africa 2002 40.0 11926563 672.
6 Zimbabwe Africa 2007 43.5 12311143 470.
It looks pretty clean and tidy. Weβll explore some additional options for looking at data sets in the coming weeks.
Make a facetted plot
Weβve used ggplot2 in the previous lessons so this will seem quite familiar. The structure of ggplot requires that we pass it an object (df), the type of geom_* we want to make (in this case a line plot), and the aesthetics (the variables we want to plot).
We can start with the plot from lesson two and assign it to the object, plot.
#set up the data like last timedf1 <- df |>group_by(continent, year) |>summarize(avg_gdpPercap =mean(gdpPercap))
`summarise()` has grouped output by 'continent'. You can override using the
`.groups` argument.
#make a plotplot <-ggplot(df1) +geom_line(aes(x = year, y = avg_gdpPercap, color = continent)) +labs(title ="Avg. GDP per Capita, by Continent", x ="Year", y ="$ (USD)")
That gives us the same line plot as last session.
Facetting will separate each of this lines into their own plot panel. You can imagine that if you have lots of data on a single plot, it is easier to see if you can separate visualizations by one of the discrete variables.
Below, the data is separated by continent. Note that the axes across each plot panel are the same which allows for comparison. This is a default of the facet_wrap() function. There are cases where you would want to set this feature to false, but in most cases it allows for obvious comparisons across the data.
Now itβs your turn practice! Below is a fully functioning code editor with starting code in place. Feel free to experiment with different grouping variables in the group_by() call or to adjust the summary statistic in summarize(). Then, have fun with the plot!